/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import EmployeeServer.Employee;
import EmployeeServer.EmployeeServer;
import EmployeeServer.EmployeeServerService;
import Entity.MyEmployee;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author HOANG ANH
 */
@ManagedBean
@SessionScoped
public final class EmployeesBean implements Serializable {

    private EmployeeServer empService;
    private String name, name2;
    private String country, country2;
    private Date birthDay, birthDay2;
    private boolean male, male2;
    private ArrayList<MyEmployee> empList = null;
    private MyEmployee employee;

    public Date getBirthDay2() {
        return birthDay2;
    }

    public void setBirthDay2(Date birthDay2) {
        this.birthDay2 = birthDay2;
    }

    public String getCountry2() {
        return country2;
    }

    public void setCountry2(String country2) {
        this.country2 = country2;
    }

    public boolean isMale2() {
        return male2;
    }

    public void setMale2(boolean male2) {
        this.male2 = male2;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public MyEmployee getEmployee() {
        return employee;
    }

    public void setEmployee(MyEmployee employee) {
        this.employee = employee;
    }

    public Date getToday() {
        Date today = new Date();
        return today;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean isMale) {
        this.male = isMale;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<MyEmployee> getEmpList() {
        if (empList == null) {
            updateList();
        }
        return empList;

    }

    public void setEmpList(ArrayList<MyEmployee> empList) {
        this.empList = empList;
    }

    public void updateList() {
        getService();
        empList = new ArrayList<MyEmployee>();
        List<Employee> list = empService.getEmpList();
        for (int i = 0; i < list.size(); i++) {
            MyEmployee em = new MyEmployee(list.get(i));
            empList.add(em);
        }
    }

    public void register() {
        try {
            Date today = new Date();
            if (name.length() < 2) {
                throw new Exception("Your name is way too short O_o");
            }
            if (country.length() < 2) {
                throw new Exception("Your country's name only have 1 syllabel ???");
            }
            if (birthDay.after(today)) {
                throw new Exception("Sorry mom. We ain't gonna hire your unborn child -.-");
            }
            if ((today.getYear() - birthDay.getYear()) > 60) {
                throw new Exception("You are too old... Please retire.");
            } else if ((today.getYear() - birthDay.getYear()) < 16) {
                throw new Exception("You are too young... Go play somewhere else.");
            }

            getService();
            Employee emp = new Employee();
            emp.setName(name);
            emp.setCountry(country);
            emp.setMale(male);

            GregorianCalendar c = new GregorianCalendar();
            c.setTime(birthDay);
            XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
            emp.setBirthday(date2);

            String output = empService.register(emp);
            if (!output.equals("OK")) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "This is not my fault. Go ask my service provider.", output);
                FacesContext.getCurrentInstance().addMessage(null, msg);
            } else {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Register completed", "Welcome to the gang");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                updateList();
                name = "";
                country = "";
                birthDay = today;
            }
        } catch (DatatypeConfigurationException ex) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERORR", ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Bang! ", ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    /** Creates a new instance of EmployeesBean */
    public EmployeesBean() {
        updateList();
        employee = new MyEmployee(new Employee());
    }

    private void getService() {
        EmployeeServerService service = new EmployeeServerService();
        empService = service.getEmployeeServerPort();
    }

    public void onSelectEvent(SelectEvent even) {
        MyEmployee selectedEmp = (MyEmployee) even.getObject();
        name2 = selectedEmp.getName();
        country2 = selectedEmp.getCountry();
        male2 = selectedEmp.isMale();
        birthDay2 = selectedEmp.getBirthdayInDate();
    }

    public void editEmployee() {
        try {
            Date today = new Date();
            if (name2.length() < 2) {
                throw new Exception("Your name is way too short O_o");
            }
            if (country2.length() < 2) {
                throw new Exception("Your country's name only have 1 syllabel???");
            }
            if (birthDay2.after(today)) {
                throw new Exception("Sorry mom. We ain't gonna hire your unborn child -.-");
            }
            if ((today.getYear() - birthDay2.getYear()) > 60) {
                throw new Exception("You are too old... Please retire.");
            } else if ((today.getYear() - birthDay2.getYear()) < 16) {
                throw new Exception("You are too young... Go play somewhere else.");
            }

            Employee emp = new Employee();
            emp.setName(name2);
            emp.setCountry(country2);
            GregorianCalendar c = new GregorianCalendar();
            c.setTime(birthDay2);
            XMLGregorianCalendar xmlc = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
            emp.setBirthday(xmlc);
            emp.setMale(male2);

            getService();
            String output = empService.updateEmployee(emp);
            if (output.equals("OK")) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Done.", "Happy now?");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                updateList();
            } else {
                throw new Exception(output);
            }
        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Uh-Oh", ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void delEmployee() {
        getService();
        String output = empService.delEmployee(name2);
        if (output.equals("OK")) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Done", "Your ass got kicked. We will miss you not.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            updateList();

        } else {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Miss!!", output);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}
